package com.wash.shoes.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.entity.BrandPo;
import com.wash.shoes.entity.ShopGoodsPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.mapper.ShopGoodsMapper;
import com.wash.shoes.page.PageFactory;
import com.wash.shoes.service.BrandService;
import com.wash.shoes.service.ShopGoodsService;
import com.wash.shoes.service.ShopService;
import com.wash.shoes.util.HeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品的表 服务实现类
 * </p>
 *
 * @author zhengyunpeng
 * @since 2024-08-26
 */
@Service
public class ShopGoodsServiceImpl extends ServiceImpl<ShopGoodsMapper, ShopGoodsPo> implements ShopGoodsService {

    @Autowired
    private ShopService shopService;

    @Autowired
    private BrandService brandService;

    @Override
    public Page<ShopGoodsPo> goodsList(ShopGoodsPo shopGoodsPo) {

        Page page = PageFactory.defaultPage();
        String shopIdStr= HeaderUtil.getShopIdStr();
        //前端说的等于adc就不走数据权限
        if(StringUtils.isNotEmpty(shopGoodsPo.getExtendInfo())&&shopGoodsPo.getExtendInfo().equals("abc")){
            shopIdStr=null;
        }
        List<ShopGoodsPo> dataList = baseMapper.goodsList(page, shopGoodsPo,shopIdStr );
        List<ShopGoodsPo> collect = dataList.stream().map(shopGoodsPo1 -> {
            BrandPo brandPo = brandService.getById(shopGoodsPo1.getBrandId());
            if (Objects.nonNull(brandPo)) {
                String brandName = brandPo.getBrandName();
                shopGoodsPo1.setBrandName(brandName);
            }
            return shopGoodsPo1;
        }).collect(Collectors.toList());

        page.setRecords(collect);
        return page;
    }

    @Override
    public ShopGoodsPo getBySkuId(String appSkuId) {
        return this.baseMapper.getBySkuId(appSkuId);
    }

    @Override
    public ShopGoodsPo getByName(String productName, Integer sellType) {
        return this.baseMapper.getByName(productName, sellType);
    }

    @Override
    public ShopGoodsPo getReturnGoods(String shoeName) {
        return this.baseMapper.getReturnGoods(shoeName);
    }

    /**
     * 商品列表-移动端的
     *
     * @param typeId 类型
     * @return
     */
    @Override

    public List<ShopGoodsPo> typeGoodsList(String typeId, Integer sellType) {
        String shopId=null;
        //当且只查询抖音来源商品时候才需要poId，美团和微信小程序不需要一城一店得过滤条件
        if(sellType == 3){
            String poId=HeaderUtil.getPoId();
            if(StringUtils.isNotEmpty(poId)){
                shopId=shopService.getShopIdByPoId(poId);
            }
        }
        return this.baseMapper.typeGoodsList(typeId, sellType, shopId);
    }


}
